// pages/detailse/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: false,
    imgurl: app.imgurl,
    list:[
      { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' }, { img: '' },{img:''},
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    app.request({
      url: 'index/user_vote_api/BabyDetails',
      method: 'post',
      type: 0,
      data: {
        program_id: options.pid,
        baby_id: options.bid
      },
      success: function (res) {
        that.setData({
          dtype: options.type,
          data: res.data,
          pid: options.pid,
          bid: options.bid
        })
      },
    })
  },
  navTo: function (e) {
    app.navTo(e.currentTarget.dataset.url)
  },
  //预览图片
  clickimg:function(e){
    var img = e.currentTarget.dataset.img;
    var arr = [];
    arr.push(img)
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: arr // 需要预览的图片http链接列表
    })
  },
  //登录
  sendRequest: function (res) {
    let that = this;
    if (!res.detail.rawData) {
      wx.showModal({
        title: '提示',
        content: '请重新点击授权',
      })
      wx.hideLoding()
      return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
    console.log(res)
    wx.login({
      success: function (code) {
        app.request({
          url: 'index/program/TowLogin',
          method: 'post',
          type: 0,
          data: {
            code: code.code,
            iv: res.detail.iv,
            encryptedData: res.detail.encryptedData
          },
          success: function (res) {
            wx.hideLoading();
            that.setData({ logo: false })
            wx.setStorageSync('userid', res.data)
          },

        })
      }
    })
  },
  toupiao:function(e){
    let that = this;
    app.request({
      url: 'index/user_vote_api/UserVotes',
      method: 'post',
      type: 1,
      data: {
        program_id: that.data.pid,
        baby_id: that.data.bid
      },
      success: function (res) {
        if (res.code == 0) {
          app.request({
            url: 'index/user_vote_api/BabyDetails',
            method: 'post',
            type: 0,
            data: {
              program_id: that.data.pid,
              baby_id: that.data.bid
            },
            success: function (ress) {
              that.setData({
                data: ress.data,
              })
            },
          })
          wx.showToast({
            title: res.msg,
            icon: 'none',
            mask: true
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            mask: true
          })
        }
      },

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.getStorage({
      key: 'userid',
      success(res) {
      },
      fail(err) {
        that.setData({ logo: true })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
		let that = this;
		app.request({
			url: 'index/program/add_share',
			method: 'post',
			type: 0,
			data: {
				baby_id: that.data.bid
			},
			success: function (res) {
				app.request({
					url: 'index/user_vote_api/BabyDetails',
					method: 'post',
					type: 0,
					data: {
						program_id: that.data.pid,
						baby_id: that.data.bid
					},
					success: function (ress) {
						that.setData({
							data: ress.data,
						})
					},
				})
			},
		})	
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '快来为小天鹅投票哦！',
      // path: '/page/user?id=123'
    }
  }
})